﻿using Microsoft.Win32;
using ServerCrafter.ClassLibrary;
using ServerCrafter.ClassLibrary.ViewModels;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.ColorEditor;

namespace ServerCrafter.WPF
{
    public partial class MainWindow
    {
        #region Server Properties

        /// <summary>
        /// Handles the SelectionChanged event of the comboboxProperties control. Changes the text in the
        /// txtboxProperty control to the value of the selected property int the comboboxProperties control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void comboboxProperties_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboboxProperties.SelectedIndex < 0) return;

            comboboxProperties.Tag = comboboxProperties.SelectedIndex;
            txtboxPropertyValue.Text = PropertiesManager.Data[(int)comboboxProperties.Tag, 1];
        }

        /// <summary>
        /// Handles the Click event of the btnSetProperties control. Sets the specified properites value to the specified value.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnSetProperties_Click(object sender, RoutedEventArgs e)
        {
            PropertiesManager.Set((int)comboboxProperties.Tag, txtboxPropertyValue.Text);
            var vmServerProperties = new VMProperties();
            comboboxProperties.ItemsSource = vmServerProperties.Get();

            comboboxProperties.SelectedIndex = (int)comboboxProperties.Tag;
        }

        /// <summary>
        /// Handles the KeyDown event of the txtboxPropertyValue control. Sets the specified properites value to the specified value.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="KeyEventArgs"/> instance containing the event data.</param>
        private void txtboxPropertyValue_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Return) return;

            PropertiesManager.Set((int)comboboxProperties.Tag, txtboxPropertyValue.Text);
            var vmServerProperties = new VMProperties();
            comboboxProperties.ItemsSource = vmServerProperties.Get();

            comboboxProperties.SelectedIndex = (int)comboboxProperties.Tag;
        }

        #endregion Server Properties

        #region User Interface Options

        /// <summary>
        /// Sets the coloreditorUIColors' color depending on what toggle button is checked.
        /// Sets the value of the checkedUiColorsButton to that of the checked toggle button.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void UiColorToggleChanged(object sender, RoutedEventArgs e)
        {
            var radioButton = (RadRadioButton)sender;
            checkedUiColorsButton = radioButton;

            if (!(radioButton.IsChecked != null && (bool)radioButton.IsChecked))
                coloreditorUIColors.SelectedColor = Colors.White;
            else
            {
                if (radioButton.Equals(toggleColorInfo)) coloreditorUIColors.SelectedColor = Settings.ServerOutputColors.Info;
                else if (radioButton.Equals(toggleColorWarning))
                    coloreditorUIColors.SelectedColor = Settings.ServerOutputColors.Warning;
                else if (radioButton.Equals(toggleColorSevere))
                    coloreditorUIColors.SelectedColor = Settings.ServerOutputColors.Severe;
                else if (radioButton.Equals(toggleColorChat))
                    coloreditorUIColors.SelectedColor = Settings.ServerOutputColors.Chat;
                else if (radioButton.Equals(toggleColorPlayerJoin))
                    coloreditorUIColors.SelectedColor = Settings.ServerOutputColors.PlayerJoin;
                else if (radioButton.Equals(toggleColorPlayerLeave))
                    coloreditorUIColors.SelectedColor = Settings.ServerOutputColors.PlayerLeave;
                else if (radioButton.Equals(toggleColorServerCrafter))
                    coloreditorUIColors.SelectedColor = Settings.ServerOutputColors.ServerCrafter;
                else if (radioButton.Equals(toggleColorOther))
                    coloreditorUIColors.SelectedColor = Settings.ServerOutputColors.Unknown;
            }
        }

        /// <summary>
        /// Handles the SelectedColorChanged event of the coloreditorUIColors control. Changes the color
        /// of a Color variable depending on the checked toggle button.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ColorChangeEventArgs"/> instance containing the event data.</param>
        private void coloreditorUIColors_SelectedColorChanged(object sender, ColorChangeEventArgs e)
        {
            if (checkedUiColorsButton == null) return;

            if (checkedUiColorsButton.Equals(toggleColorInfo)) Settings.ServerOutputColors.Info = e.Color;
            else if (checkedUiColorsButton.Equals(toggleColorWarning)) Settings.ServerOutputColors.Warning = e.Color;
            else if (checkedUiColorsButton.Equals(toggleColorSevere)) Settings.ServerOutputColors.Severe = e.Color;
            else if (checkedUiColorsButton.Equals(toggleColorChat)) Settings.ServerOutputColors.Chat = e.Color;
            else if (checkedUiColorsButton.Equals(toggleColorPlayerJoin)) Settings.ServerOutputColors.PlayerJoin = e.Color;
            else if (checkedUiColorsButton.Equals(toggleColorPlayerLeave)) Settings.ServerOutputColors.PlayerLeave = e.Color;
            else if (checkedUiColorsButton.Equals(toggleColorServerCrafter)) Settings.ServerOutputColors.ServerCrafter = e.Color;
            else if (checkedUiColorsButton.Equals(toggleColorOther)) Settings.ServerOutputColors.Unknown = e.Color;

            Settings.Apply.ServerOutputColors();
        }

        #endregion User Interface Options

        #region Server Launch Options

        #region Server Java Executable

        /// <summary>
        /// Handles the Click event of the btnEditJarFileLocation control. Opens a file dialog to select
        /// the custom server Java executable.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnEditJarFileLocation_Click(object sender, RoutedEventArgs e)
        {
            var fileDialog = new OpenFileDialog
            {
                Filter = @"Java Executables|*.jar",
                Title = @"Select the server Java executable",
                CheckFileExists = true,
                CheckPathExists = true
            };

            if (fileDialog.ShowDialog() == false) return;

            txtboxJarFileLocation.Text = fileDialog.FileName;
            if (toggleJarLocationCustom.IsChecked != null && (bool)toggleJarLocationCustom.IsChecked)
                Settings.ServerStartInfo.FileName = fileDialog.FileName;
            Settings.Apply.ServerStartInfo();
        }

        /// <summary>
        /// Handles the Click event of the toggleJarLocationCraftBukkit control. Changes the FileName variable's
        /// value to the location of the CraftBukkit.jar file.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void toggleJarLocationCraftBukkit_Click(object sender, RoutedEventArgs e)
        {
            if (toggleJarLocationCustom.IsChecked != null && (bool)toggleJarLocationCustom.IsChecked)
                Settings.ServerStartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
                                         "\\ServerCrafter\\CraftBukkit.jar";
            Settings.Apply.ServerStartInfo();
        }

        /// <summary>
        /// Handles the Click event of the toggleJarLocationVanilla control. Changes the FileName variable's
        /// value to the location of the Vanilla.jar file.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void toggleJarLocationVanilla_Click(object sender, RoutedEventArgs e)
        {
            if (toggleJarLocationVanilla.IsChecked != null && (bool)toggleJarLocationVanilla.IsChecked)
                Settings.ServerStartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
                                         "\\ServerCrafter\\Vanilla.jar";
            Settings.Apply.ServerStartInfo();
        }

        /// <summary>
        /// Handles the Click event of the toggleJarLocationVanillaForge control. Changes the FileName variable's
        /// value to the location of the VanillaForge.jar file.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void toggleJarLocationVanillaForge_Click(object sender, RoutedEventArgs e)
        {
            if (toggleJarLocationVanillaForge.IsChecked != null && (bool)toggleJarLocationVanillaForge.IsChecked)
                Settings.ServerStartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
                                         "\\ServerCrafter\\VanillaForge.jar";
            Settings.Apply.ServerStartInfo();
        }

        /// <summary>
        /// Handles the Click event of the toggleJarLocationCustom control. Changes the FileName variable's
        /// value to the specified text of the textboxJarFileLocation control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void toggleJarLocationCustom_Click(object sender, RoutedEventArgs e)
        {
            if (toggleJarLocationCustom.IsChecked == null || !((bool)toggleJarLocationCustom.IsChecked)) return;

            Settings.ServerStartInfo.FileName = txtboxJarFileLocation.Text;
            Settings.Apply.ServerStartInfo();
        }

        /// <summary>
        /// Handles the TextChanged event of the txtboxJarFileLocation control. Changes the value of the FileName variable to
        /// the text of the txtboxJarFileLocation control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="TextChangedEventArgs"/> instance containing the event data.</param>
        private void txtboxJarFileLocation_TextChanged(object sender, TextChangedEventArgs e)
        {
            Settings.ServerStartInfo.FileName = txtboxPropertyValue.Text; Settings.Apply.ServerStartInfo();
        }

        #endregion Server Java Executable

        #region Additional Arguments

        /// <summary>
        /// Handles the TextChanged event of the txtboxAdditionalParams control. Changes the Arguments variable's
        /// value to the text of the txtboxAdditionalParams control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="TextChangedEventArgs"/> instance containing the event data.</param>
        private void txtboxAdditionalParams_TextChanged(object sender, TextChangedEventArgs e)
        {
            Settings.ServerStartInfo.Arguments = txtboxAdditionalParams.Text; Settings.Apply.ServerStartInfo();
        }

        private void txtboxWorkingDirectory_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            Settings.ServerStartInfo.WorkingDirectory = txtboxWorkingDirectory.Text; Settings.Apply.ServerStartInfo();
        }

        private void btnEditWorkingDirectory_Click(object sender, RoutedEventArgs e)
        {
            var folderDialog = new System.Windows.Forms.FolderBrowserDialog
                {
                    Description = @"Selected the folder where all the server files will be generated or loaded from",
                    ShowNewFolderButton = true
                };
            if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) return;

            txtboxWorkingDirectory.Text = folderDialog.SelectedPath;
            Settings.ServerStartInfo.WorkingDirectory = folderDialog.SelectedPath;
            Settings.Apply.ServerStartInfo();
        }

        #endregion Additional Arguments

        #region RAM Allocation

        /// <summary>
        /// Handles the ValueChanged event of the sliderInitialRamAmount control. Change the text of the
        /// txtboxInitialRamAmount control to the value of the sliderInitialRamAmount control.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void sliderInitialRamAllocation_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            txtboxInitialRamAllocation.Text = Convert.ToString((int)sliderInitialRamAllocation.Value);
        }

        /// <summary>
        /// Handles the ValueChanged event of the sliderMaximumRamAmount control. Change the text of the
        /// txtboxMaximumRamAmount control to the value of the sliderMaximumRamAmount control.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void sliderMaximumRamAllocation_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            txtboxMaximumRamAllocation.Text = Convert.ToString((int)sliderMaximumRamAllocation.Value);
        }

        /// <summary>
        /// Handles the TextChanged event of the txtboxInitialRamAllocation control. Changes the value of the
        /// sliderInitialRamAmountAllocation and the value of the InitialRam variable to the text of the
        /// txtboxInitialRamAllocation text.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="TextChangedEventArgs"/> instance containing the event data.</param>
        private void txtboxInitialRamAllocation_TextChanged(object sender, TextChangedEventArgs e)
        {
            sliderInitialRamAllocation.Value = Convert.ToInt32(txtboxInitialRamAllocation.Text);
            Settings.ServerStartInfo.InitialRam = Convert.ToInt32(txtboxInitialRamAllocation.Text);
            Settings.Apply.ServerStartInfo();
        }

        /// <summary>
        /// Handles the TextChanged event of the txtboxMaximumRamAllocation control. Changes the value of the
        /// sliderMaximumRamAmountAllocation and the value of the MaximumRam variable to the text of the
        /// txtboxMaximumRamAllocation text.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="TextChangedEventArgs"/> instance containing the event data.</param>
        private void txtboxMaximumRamAllocation_TextChanged(object sender, TextChangedEventArgs e)
        {
            sliderMaximumRamAllocation.Value = Convert.ToInt32(txtboxMaximumRamAllocation.Text);
            Settings.ServerStartInfo.MaximumRam = Convert.ToInt32(txtboxMaximumRamAllocation.Text);
            Settings.Apply.ServerStartInfo();
        }

        #endregion RAM Allocation

        #endregion Server Launch Options
    }
}